export default {
  data() {
    return {
      price: "",
    };
  },
  methods: {
    // 金额转换成汉字
    numberToChineseMoney(price) {
      price = parseFloat(price); // 将输入的值转换为浮点数
      if (isNaN(price)) {
        return "无效金额"; // 如果无法转换为数字，返回错误信息或其他处理
      }
      const capitalNumbers = "零壹贰叁肆伍陆柒捌玖";
      const unit = [
        "元",
        "拾",
        "佰",
        "仟",
        "万",
        "拾",
        "佰",
        "仟",
        "亿",
        "拾",
        "佰",
      ];
      const decimalUnit = ["角", "分"];

      const moneyArray = price.toFixed(2).split(".");
      const integerPart = moneyArray[0];
      const decimalPart = moneyArray[1];

      let chineseCapital = "";

      for (let i = 0; i < integerPart.length; i++) {
        // 整数部分
        const num = integerPart[i];
        if (num !== "0") {
          chineseCapital +=
            capitalNumbers[parseInt(num)] + unit[integerPart.length - 1 - i];
        } else if (i < integerPart.length - 1 && integerPart[i + 1] !== "0") {
          chineseCapital += capitalNumbers[parseInt(num)];
        } else if (i === integerPart.length - 1) {
          chineseCapital += unit[0];
        }
      }
      // 小数部分，处理整数部分末尾
      if (decimalPart === "00") {
        chineseCapital += "整";
      } else if (decimalPart !== "00") {
        for (let i = 0; i < decimalPart.length; i++) {
          const num = decimalPart[i];
          if (num !== "0") {
            chineseCapital += capitalNumbers[parseInt(num)] + decimalUnit[i];
          }
        }
      }

      return chineseCapital;
    },
  },
};
